/**
 * Created by XGJ on 2017/9/25.
 */

import {Component} from "@angular/core";
import {UserInFo} from "../../model/userInfo";
import {FormGroup, FormBuilder, Validators} from "@angular/forms";
import {LoginService} from "./login.Service";
import {AlertController, ViewController, ToastController} from "ionic-angular";

@Component({
    selector: 'page-login-model',
    templateUrl: 'login.html'
})

export class LoginModelPage{
    userInfo:UserInFo;
    loginForm: FormGroup;
    user_phone: any;
    phone_code: any;
    constructor(
        private loginService:LoginService,
        private formBuilder:FormBuilder,
        private alertCtrl:AlertController,
        private viewCtrl:ViewController,
        private toastCtrl:ToastController
    ){
        //不知道这些是干啥的？

        this.loginForm = formBuilder.group({
            user_phone: ['18801229513', Validators.compose([Validators.minLength(11), Validators.maxLength(11), Validators.required, Validators.pattern("^(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$")])],
            phone_code: ['123456', Validators.compose([Validators.required, Validators.minLength(6)])]
        });
        this.user_phone = this.loginForm.controls['user_phone'];
        this.phone_code = this.loginForm.controls['phone_code'];
    }

    sendCode(){
        let alert = this.alertCtrl.create({
            title: '提示',
            subTitle: '验证码发送成功',
            message:'您的验证码是123456',
            buttons: ['知道了']
        });
        alert.present();  //这是什么方法。
    }
    login(value){
        if(value.phone_code == '123456'){
            this.loginService.login().then(result =>{
                if(result['success']){
                    let data=result['data'];
                    let userInfo = <UserInFo>data;
                    this.userInfo = userInfo;
                    this.viewCtrl.dismiss(userInfo);
                }
            })
        }else{
            let toast = this.toastCtrl.create({
                message: '验证码这样都能输如错误，你是不是虎',
                duration: 2000,
                position:'middle'
            });
            toast.present();
        }
    }
    dismiss() {
        this.viewCtrl.dismiss(new UserInFo());
    }
}